<?php

$objXajax->registerFunction("frmconfigure_loadConfig");
$objXajax->registerFunction("frmconfigure_save");
$objXajax->registerFunction("frmconfigure_edit");  
$objXajax->registerFunction("frmconfigure_loadHotel");  
$objXajax->registerFunction("frmconfigure_InitWard");  
$objXajax->registerFunction("frmconfigure_InitDistrict");  
$objXajax->registerFunction("frmconfigure_InitCity");  
$objXajax->registerFunction("frmconfigurehotel_save");  

function frmconfigure_save($params){
 global $objDbUpdate,$hotel_id,$user_id;
 $objResponse = new xajaxResponse();
 $flg_fee = $params['chkFee'];
 $fee_saturday = $params['txtSatFee'];
 $fee_sunday = $params['txtSunFee'];
 $flg_productfee   = $params['chkProductFee'];
 $flg_roomfee   = $params['chkRoomFee'];
 $flg_sms  = $params['chkSMS'];
 $send_time  = $params['cbbSendtime'];
 $flg_inday  = $params['rdInday']=='on'?1:0;
 $flg_bycheckout  = $params['chkTimeCheckOut'];
 $timestart  = $params['cbbTimeBegin'];
 $timeend  = $params['cbbTimeCheckOut'];
 $flg_promo  = $params['chkPromo'];
 $percent_promo  = $params['txtPercenPromo'];
 $flg_productpromo  = $params['chkProductPromo'];
 $flg_roompromo  = $params['chkRoomPromo'];
 $phonerecive = $params['txtPhoneRecieve'];
 $sqlUpdate = "UPDATE tbl_config SET flg_fee='$flg_fee',fee_saturday='$fee_saturday',fee_sunday='$fee_sunday',flg_productfee='$flg_productfee',flg_roomfee='$flg_roomfee',phonerecieve='$phonerecive',
                flg_sms='$flg_sms',send_time='$send_time',flg_inday='$flg_inday',flg_bycheckout='$flg_bycheckout',timestart='$timestart'
                ,timeend='$timeend',flg_promo='$flg_promo',percent_promo='$percent_promo',flg_productpromo='$flg_productpromo',flg_roompromo = '$flg_roompromo',user_update = '$user_id', date_update = now()  WHERE hotel_id = '$hotel_id' ";
               // echo $sqlUpdate; die();
 $objDbUpdate->Execute($sqlUpdate);
 $objResponse->addScript('xajax_SetConfigure();');    
 $objResponse->addScript('changeWaiting(1);');    
 return   $objResponse->getXML($sqlInsert);
}

function frmconfigure_loadConfig(){     
global $lang,$objDbSelect,$hotel_id;
$objResponse = new xajaxResponse();
$sql= "select * from  tbl_config WHERE hotel_id = '$hotel_id'";

$arr= $objDbSelect->GetArray($sql);
 
 $flg_fee = $arr[0]['flg_fee'];
 $fee_saturday = $arr[0]['fee_saturday'];
 $fee_sunday = $arr[0]['fee_sunday'];

 $flg_sms  = $arr[0]['flg_sms'];
 $send_time  = $arr[0]['send_time'];
 $flg_inday  = $arr[0]['flg_inday'];
 $flg_bycheckout  = $arr[0]['flg_bycheckout'];
 $timestart  = $arr[0]['timestart'];
 $timeend  = $arr[0]['timeend'];
 $flg_promo  = $arr[0]['flg_promo'];
 $percent_promo  = $arr[0]['percent_promo'];
 $flg_roompromo  = $arr[0]['flg_roompromo'];
 $flg_productpromo  = $arr[0]['flg_productpromo'];
 $flg_productfee  = $arr[0]['flg_productfee'];
 $flg_roomfee  = $arr[0]['flg_roomfee'];
 $phonerecieve  = $arr[0]['phonerecieve'];
 
 if($flg_fee==1){
    $objResponse->addScript("$('#frmconfigure_chkFee').attr('checked','checked')"); 
    $objResponse->addScript("$('#frmconfigure_tblFee').css('display','')"); 
    $objResponse->addScript("$('#frmconfigure_txtSatFee').val('$fee_saturday')"); 
    $objResponse->addScript("$('#frmconfigure_txtSunFee').val('$fee_sunday')");
    $objResponse->addScript("$('#frmconfigure_txtPhoneRecieve').val('$phonerecieve')");
    
    if($flg_productfee==1){
      $objResponse->addScript("$('#frmconfigure_chkProductFee').attr('checked','checked')");   
    }else{
       $objResponse->addScript("$('#frmconfigure_chkProductFee').attr('checked',false)");   
    }
    if($flg_roomfee==1){
      $objResponse->addScript("$('#frmconfigure_chkRoomFee').attr('checked','checked')");   
    }else{
       $objResponse->addScript("$('#frmconfigure_chkRoomFee').attr('checked',false)");   
    }
 }else{
    $objResponse->addScript("$('#frmconfigure_chkFee').attr('checked',false)"); 
    $objResponse->addScript("$('#frmconfigure_tblFee').css('display','none')"); 
    $objResponse->addScript("$('#frmconfigure_txtSatFee').val('0')"); 
    $objResponse->addScript("$('#frmconfigure_txtSunFee').val('0')");
    $objResponse->addScript("$('#frmconfigure_txtPhoneRecieve').val('')");
  }  
 
  if($flg_sms==1){
    $objResponse->addScript("$('#frmconfigure_chkSMS').attr('checked','checked')"); 
    $objResponse->addScript("$('#frmconfigure_tblSMS').css('display','')"); 
    $objResponse->addScript("$('#frmconfigure_cbbSendtime').val('$send_time')"); 
    
    if($flg_inday==1){
      $objResponse->addScript("$('#frmconfigure_rdInday').attr('checked','checked')");   
    }else{
      $objResponse->addScript("$('#frmconfigure_rdUpToNow').attr('checked','checked')");     
    }
 }else{
    $objResponse->addScript("$('#frmconfigure_chkSMS').attr('checked',false)"); 
    $objResponse->addScript("$('#frmconfigure_tblSMS').css('display','none')"); 
    $objResponse->addScript("$('#frmconfigure_cbbSendtime').val(7)"); 
    
    if($flg_inday==1){
      $objResponse->addScript("$('#frmconfigure_rdInday').attr('checked','checked')");   
    }else{
      $objResponse->addScript("$('#frmconfigure_rdUpToNow').attr('checked','checked')");     
    }  
 }
 
  if($flg_promo==1){
    $objResponse->addScript("$('#frmconfigure_chkPromo').attr('checked','checked')"); 
    $objResponse->addScript("$('#frmconfigure_tblPromo').css('display','')");  
    $objResponse->addScript("$('#frmconfigure_txtPercenPromo').val('$send_time')"); 
    
    if($flg_productpromo==1){
      $objResponse->addScript("$('#frmconfigure_chkProductPromo').attr('checked','checked')");   
    }else{
      $objResponse->addScript("$('#frmconfigure_chkProductPromo').attr('checked',false)");     
    }
    if($flg_roompromo==1){
      $objResponse->addScript("$('#frmconfigure_chkRoomPromo').attr('checked','checked')");   
    }else{
      $objResponse->addScript("$('#frmconfigure_chkRoomPromo').attr('checked',false)");     
    }
    
 }else{
    $objResponse->addScript("$('#frmconfigure_chkPromo').attr('checked',false)"); 
    $objResponse->addScript("$('#frmconfigure_tblPromo').css('display','none')");  
    $objResponse->addScript("$('#frmconfigure_txtPercenPromo').val(0)"); 
 } 
 
 if($flg_bycheckout==1){
    $objResponse->addScript("$('#frmconfigure_chkTimeCheckOut').attr('checked','checked')"); 
    $objResponse->addScript("$('#frmconfigure_tblTimeCheckOut').css('display','')");    
    $objResponse->addScript("$('#frmconfigure_cbbTimeBegin').val('$timestart')"); 
    $objResponse->addScript("$('#frmconfigure_cbbTimeCheckOut').val('$timeend')"); 
    
 }else{
      $objResponse->addScript("$('#frmconfigure_chkTimeCheckOut').attr('checked',false)");
    $objResponse->addScript("$('#frmconfigure_tblTimeCheckOut').css('display','none')"); 
    $objResponse->addScript("$('#frmconfigure_cbbTimeBegin').val(7)"); 
    $objResponse->addScript("$('#frmconfigure_cbbTimeCheckOut').val(12)"); 

 }                           
                           
$objResponse->addScript('changeWaiting(1);');                                                      
return $objResponse->getXML();
}

function frmconfigure_loadHotel(){     
global $lang,$objDbSelect,$hotel_id;
$objResponse = new xajaxResponse();

$sql= "select * FROM tbl_service WHERE id = '$hotel_id'";
//echo $sql; die();
$arr= $objDbSelect->GetArray($sql); 
 $name = $arr[0]['name'];
 $ward_id = $arr[0]['ward_id'];
 $address = $arr[0]['address'];
 $district_id   = $arr[0]['district_id'];
 $city_id  = $arr[0]['city_id'];
 $logo  = $arr[0]['logo'];
 $level  = $arr[0]['level'];
 $star  = $arr[0]['star'];
 $boss  = $arr[0]['boss'];
 $phone  = $arr[0]['phone'];
 $lat  = $arr[0]['lat'];
 $long  = $arr[0]['lng'];

 $objResponse->addScript("$('#frmconfigurehotel_txthotelname').val('$name');");                         
 $objResponse->addScript("$('#frmconfigurehotel_cbbCity').val('$city_id');");                         
 $objResponse->addScript("$('#frmconfigurehotel_tdDistrict').val('$district_id');");                           
 $objResponse->addScript("$('#frmconfigurehotel_Ward').val('$ward_id');");                           
 $objResponse->addScript("$('#frmconfigurehotel_txtAddress').val('$address');");                           
 $objResponse->addScript("$('#frmconfigurehotel_boss').val('$boss');");                           
 $objResponse->addScript("$('#frmconfigurehotel_txtPhone').val('$phone');");
 $objResponse->addScript('createMarker('.$lat.','.$long.');');                           
$objResponse->addScript('changeWaiting(1);'); 
$objResponse->addScript("xajax_frmconfigure_InitCity($city_id);");
$objResponse->addScript("xajax_frmconfigure_InitDistrict($city_id,$district_id);");
$objResponse->addScript("xajax_frmconfigure_InitWard($district_id,$ward_id);"); 
return $objResponse->getXML();
}

function  frmconfigure_Getconfigure(){
global $objDbSelect;
$sqlSelect = 'select * from tbl_service_configure ';
return $objDbSelect->GetArray($sqlSelect);
}

function  frmconfigure_edit($id){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $sql         = "select * from tbl_service_configure where id = '$id'";
    $arrconfigure         = $objDbSelect->GetArray($sql);
    
     $sql         = "select * from tbl_mainconfigure_powertype where hotel_configure_id = '$id'";
     $arrPower         = $objDbSelect->GetArray($sql);            
    $name_vi = $arrconfigure[0]['name_vi'];
    $name_en = $arrconfigure[0]['name_en'];
    $strId = '';
    for($i=0;$i<count($arrPower);$i++){
      $strId .= ','.$arrPower[$i]['power_type_id'] ; 
    }
    if(strlen($strId)>0){
        $strId= substr($strId,1);
    }
    $objResponse->addScript("setPower('$strId');");
    
    $objResponse->addScript("$('#frmconfigure_txtViconfigure').val('$name_vi');");
    $objResponse->addScript("$('#frmconfigure_txtEnconfigure').val('$name_en');");
    $objResponse->addScript("fn_SubChkChange('frmconfigure_chkAllPower','frmconfigure_chkpower');");    
    $objResponse->addScript('changeWaiting(1);');
    
    return $objResponse->getXML();
} 
function frmconfigure_InitCity($city_id=''){
    $objResponse = new xajaxResponse();
    $cbbCity = frmconfigure_loadCity('frmconfigurehotel_cbbCity',$city_id);
     $objResponse->addAssign('frmconfigurehotel_tdCity', 'innerHTML',$cbbCity);
    return $objResponse->getXML();
}
function frmconfigure_InitDistrict($city_id,$district_id=''){
    $objResponse = new xajaxResponse();
    $cbbDistrict = frmconfigure_loadDistrict($city_id,'frmconfigurehotel_cbbDistrict',$district_id);
     $objResponse->addAssign('frmconfigurehotel_tdDistrict', 'innerHTML',$cbbDistrict);
    return $objResponse->getXML();
}
function frmconfigure_InitWard($district_id,$ward_id=''){
    $objResponse = new xajaxResponse();
    $cbbWard = frmconfigure_loadWard($district_id,'frmconfigurehotel_cbbWard',$ward_id);
     $objResponse->addAssign('frmconfigurehotel_tdWard', 'innerHTML',$cbbWard);
    return $objResponse->getXML();
}    
function frmconfigure_loadCity($id,$city_id=''){
    global $objDbSelect,$lang;
    $condition = '';
    $sql =  " select id as id, name as name from tbl_city where active = 1 ";
    $arr = $objDbSelect->GetArray($sql);
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $addselect ='';
         if($value==$city_id){
            $addselect = ' selected="selected" ';
        }
        $strResult .= "<option value ='$value' $addselect>$name</option>";
    }

    $firstOption = '';
    if($all){
      $firstOption = "<option value ='' > Tất cả </option> "; 
    }else{
      $firstOption = "<option value ='' > Chọn </option> ";   
    }
    $strResult ="<select id='$id' class='width100 cbbRequired'>$firstOption".$strResult.'</select>';  
    return   $strResult;
}
function frmconfigure_loadDistrict($city_id,$id,$district_id=''){
    global $objDbSelect,$lang;
    $condition = '';
    $powertypeid =1;
    $sql =  " select id as id, name as name from tbl_district where active = 1 and city_id = '$city_id'";
    $arr = $objDbSelect->GetArray($sql);
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $addselect='';
        if($value==$district_id){
            $addselect = ' selected="selected" ';
        }
        $strResult .= "<option value ='$value' $addselect>$name</option>";
    }
    $firstOption = "<option value ='' > Chọn </option> ";   
    $strResult ="<select id='$id' class='cbbRequired width100'>$firstOption".$strResult.'</select>';
     
    return $strResult;       
}
function frmconfigure_loadWard($district_id,$id,$ward_id=''){
    global $objDbSelect,$lang;
    $condition = '';
    $powertypeid =1;
    $sql =  " select id as id, name as name from tbl_ward where active = 1 and district_id = '$district_id'";
    $arr = $objDbSelect->GetArray($sql);
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $addselect='';
        if($value==$ward_id){
            $addselect = ' selected="selected" ';
        }
        $strResult .= "<option value ='$value' $addselect>$name</option>";
    }
    $firstOption = "<option value =''> Chọn </option> ";   
    $strResult ="<select id='$id' class='width100 cbbRequired'>$firstOption".$strResult.'</select>';
     
    return $strResult;       
}
function frmconfigurehotel_save($params){

 global $objDbUpdate;
 $hotel_id = $_SESSION['current']['hotel_id'];
 $user_id = $_SESSION['current']['user_id'];
 $objResponse = new xajaxResponse();
 $name = addslashes($params['txthotelname']);
 $city_id = $params['cbbCity'];
 $district_id = $params['cbbDistrict'];
 $ward_id   = $params['cbbWard'];
 $address   = addslashes($params['txtAddress']);
 $boss  = addslashes($params['boss']);
 $phone  = $params['txtPhone'];
 $hdlat  = $params['hdlat'];
 $hdlng  = $params['hdlng']; 
 $sqlUpdate = "UPDATE tbl_service SET ward_id='$ward_id', address='$address', name='$name', district_id='$district_id', city_id='$city_id', logo='', boss='$boss', lat='$hdlat', lng='$hdlng', date_update = now(),user_update='$user_id'  WHERE id = '$hotel_id' ";
 //echo $sqlUpdate; die();
 $objDbUpdate->Execute($sqlUpdate);
// $objResponse->addScript('xajax_SetConfigure();');    
 $objResponse->addScript('changeWaiting(1);');    
 return   $objResponse->getXML($sqlUpdate);
}
  
  # end region process data
?>
